package com.example.shopingcenter;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;

import com.example.shopingcenter.DateBase.ShoppingDbHelper;
import com.example.shopingcenter.enity.GoodsInfo;
import com.example.shopingcenter.usercenter.StartActivity;
import com.example.shopingcenter.util.FileUtil;
import com.example.shopingcenter.util.SharedUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class MyApplication extends Application {

    private static MyApplication mApp;

    public int goodsCount;

    public static MyApplication getmApp() {
        return mApp;
    }

    public void onCreate() {

        super.onCreate();
        mApp=this;

        initGoodsInfo();


    }

    private void initGoodsInfo() {

          boolean isFirst= SharedUtil.getmUtil(this).readBoolean("first",true);
          String directory=getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+ File.separatorChar;
          if (isFirst){
              List<GoodsInfo>list=GoodsInfo.getDefaultList();

              for (GoodsInfo info:list){
                  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), info.pic);
                  String path=directory+info.id+".jpg";
                  FileUtil.saveImage(path,bitmap);

                  bitmap.recycle();
                  info.picPath=path;
              }
              ShoppingDbHelper dbHelper=ShoppingDbHelper.getDbHelper(this);
              dbHelper.openWriteLink();
              dbHelper.insertGoodsInfos(list);
              dbHelper.closeLink();
              SharedUtil.getmUtil(this).writeBoolean("first",false);
          }
    }

}